package com.woniu.p2p.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.p2p.entity.UserZong;
import com.woniu.p2p.mapper.UserZongMapper;
import com.woniu.p2p.service.MymoneyService;
import com.woniu.p2p.vo.MymoneyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MymoneyServiceImpl implements MymoneyService {

    @Autowired
    UserZongMapper userZongMapper;

    @Override
    public MymoneyVo findAllMyMoney(Integer id) {
        MymoneyVo allMyMoney1 = userZongMapper.findAllMyMoney(id);
        MymoneyVo allChargeMoney = userZongMapper.findAllChargeMoney(id);
        if (allChargeMoney == null){
            return null;
        }
        long userchargemoney = allChargeMoney.getUserchargemoney();
        long useraftergetmoney = allMyMoney1.getUseraftergetmoney();
        long usergetmoney = allMyMoney1.getUsergetmoney();
        long userbeforegetmoney=usergetmoney-useraftergetmoney;
        allMyMoney1.setUserchargemoney(userchargemoney);
        allMyMoney1.setUserbeforegetmoney(userbeforegetmoney);
        return allMyMoney1;
    }

    @Override
    public PageInfo<UserZong> findAllmymoneyHistory(Integer id,Integer start, Integer num) {
        PageHelper.startPage(start,100);
        List<UserZong> list = userZongMapper.findAllMyMoneyHistory(id);
        PageInfo<UserZong> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<UserZong> findAllMyMoneyByKeywordAndTime(Integer id, Integer start, Integer num, String keyword, Date predate, Date nextDate) {
        if(predate!=null && nextDate!=null){
            PageHelper.startPage(start,100);
            java.sql.Date date = new java.sql.Date(predate.getTime());
            java.sql.Date date1 = new java.sql.Date(nextDate.getTime());
            List<UserZong> list = userZongMapper.findAllMyMoneyByKeywordAndTime(id, keyword, date, date1);
            PageInfo<UserZong> pageInfo=new PageInfo<>(list);
            System.out.println(pageInfo);
            return pageInfo;
        }else{
            PageHelper.startPage(start,100);
            List<UserZong> list = userZongMapper.findAllMyMoneyByKeywordAndTime(id, keyword, predate, nextDate);
            PageInfo<UserZong> pageInfo=new PageInfo<>(list);
            System.out.println(pageInfo);
            return pageInfo;
        }

    }

    @Override
    public UserZong selectbyid(Integer zongId) {
        return userZongMapper.selectByPrimaryKey(zongId);
    }
}
